<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
        offset="50"
      >
        <ArticleItem
          v-for="obj in list"
          :key="obj.art_id"
          :artObj="obj"
          @dislikeEv="dislikeFn"
          @reportEv="reportFn"
          @click.native="itemClickFn(obj.art_id)"
        ></ArticleItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import {
  getAllArticleListAPI,
  dislikeArticleAPI,
  reportArticleAPI
} from '@/api'
import ArticleItem from '../../../components/ArticleItem.vue'
// import { Notify } from 'vant'
import Notify from '@/ui/Notify.js'
export default {
  components: {
    ArticleItem
  },
  props: {
    channelId: Number
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      getTime: new Date().getTime(),
      isLoading: false
    }
  },
  methods: {
    async getAllArticleListFn () {
      const res = await getAllArticleListAPI({
        channel_id: this.channelId,
        timestamp: this.getTime
      })
      this.list = [...this.list, ...res.data.data.results]
      this.getTime = res.data.data.pre_timestamp
      this.loading = false
      if (res.data.data.pre_timestamp === null) {
        this.finished = true
      }
      this.isLoading = false
    },
    onLoad () {
      // 双保险和上面 immediate-check 效果一样
      if (this.list.length === 0) {
        this.loading = false
        return
      }
      this.getAllArticleListFn()
    },
    onRefresh () {
      this.list = []
      this.getTime = new Date().getTime()
      this.getAllArticleListFn()
    },
    async dislikeFn (target) {
      await dislikeArticleAPI({ target })
      Notify({ type: 'success', message: '反馈成功' })
    },
    async reportFn (target, type) {
      await reportArticleAPI({ target, type })
      Notify({ type: 'success', message: '举报成功' })
    },
    itemClickFn (id) {
      this.$router.push({
        path: `/detail?art_id=${id}`
      })
    }
  },
  created () {
    this.getAllArticleListFn()
  }
}
</script>

<style></style>
